<template>
  <div class="table">
    <el-table
        :data="props.list"
        style="width: 100%"
        :row-class-name="tableRowClassName"
    >
      <el-table-column prop="course_img" label="图片" width="180">
        <template #default="scope">
          <img :src="scope.row.course_img" class="courseImg-img" alt="课程图片">
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="180" />
      <el-table-column prop="price" label="价格" />
      <el-table-column prop="point" label="评分" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button
              type="primary"
              size="small"
              @click="props.editClick(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm title="确定删除该课程？" @confirm="props.deleteHandle(scope.row.id)">
            <template #reference>
              <el-button
                  type="danger"
                  size="small"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
const props = defineProps({
  list: {
    type: Array,
    required: true,
    default: () => []
  },
  editClick: {
    type: Function,
    required: true
  },
  deleteHandle: {
    type: Function,
    required: true
  }
});

/**
 * 定义每行课程的样式
 */
const tableRowClassName = ({rowIndex}) => {
  if (rowIndex % 2 === 0) {
    return 'warning-row'
  } else {
    return ''
  }
}
</script>

<style scoped lang="less">
.courseImg-img {
  width: 150px;
  height: 100px;
  object-fit: cover;
}

.table {
  width: 100%;
  margin-bottom: 20px;
}
</style>